//
//  WHYMainViewController.m
//  News163
//
//  Created by 王洪运 on 15/12/29.
//  Copyright © 2015年 王洪运. All rights reserved.
//

#import "WHYMainViewController.h"
#import "Masonry.h"
#import "WHYChannel.h"
#import "WHYHeadLineViewController.h"
#import "WHYSocietyViewController.h"
#import "WHYHomeLabel.h"
#import "WHYLeftDockMenu.h"
#import "UIViewController+WHYExtension.h"
#import "WHYPictureViewController.h"
#import "WHYNavigationViewController.h"

typedef NS_ENUM(NSUInteger, WHYLeftDockMenuType) {
    WHYLeftDockMenuTypeNews = 0,
    WHYLeftDockMenuTypeReading,
    WHYLeftDockMenuTypePhoto,
    WHYLeftDockMenuTypeVideo,
    WHYLeftDockMenuTypeComment,
    WHYLeftDockMenuTypeRadio
};

@interface WHYMainViewController ()<UIScrollViewDelegate>

//存放频道列表的滚动视图
@property (strong, nonatomic) UIScrollView *listScrollView;
//存放内容的滚动视图
@property (strong, nonatomic) UIScrollView *contentScrollView;

@property (weak, nonatomic) WHYLeftDockMenu *leftDockMenu;

@property (strong, nonatomic) NSArray<WHYChannel *> *channelList;

@end

@implementation WHYMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self setupChildViewControllers];
    
    [self setupTitles];
    
    [self setupScrolViewProperties];
    
    [self setupDefaultViewController];
    
    WHYHomeLabel *label =  self.listScrollView.subviews.firstObject;
    label.scale = 1.0;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sidebar_nav_news"] style:UIBarButtonItemStylePlain target:self action:@selector(clickLeftBarButtonItem)];
    
    [self setupNotification];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 监听通知
-(void)setupNotification {
    
    [WHYDefaultNotificationCenter addObserver:self selector:@selector(clickLeftDockMenuButton:) name:WHYLeftDockMenuDidSelectedNotification object:nil];
    
}

-(void)dealloc {
    [WHYDefaultNotificationCenter removeObserver:self];
}

-(void)clickLeftDockMenuButton:(NSNotification *)notification {
    
    int index = [notification.userInfo[WHYLeftDockMenuSelectedIndexKey] intValue];
    
    [self switchChildViewController:index];
}

-(void)switchChildViewController:(int)index {
    
    switch (index) {
        case WHYLeftDockMenuTypeNews:
            NSLog(@"新闻");
            break;
            
        case WHYLeftDockMenuTypeReading:
            NSLog(@"订阅");
            break;
            
        case WHYLeftDockMenuTypePhoto:
        {
            WHYNavigationViewController *pictureNav = [WHYNavigationViewController viewControllerWithStoryboardName:@"Picture" bundle:nil identifier:nil];
            [self presentViewController:pictureNav animated:YES completion:nil];
            
            break;
        }
            
        case WHYLeftDockMenuTypeVideo:
            NSLog(@"视频");
            break;
            
        case WHYLeftDockMenuTypeComment:
            NSLog(@"跟帖");
            break;
            
        case WHYLeftDockMenuTypeRadio:
            NSLog(@"电台");
            break;
            
        default:
            break;
    }
    
}


#pragma mark - 左边抽屉
-(void)clickLeftBarButtonItem {
    
    if (self.leftDockMenu) {
        
        if (self.contentScrollView.contentOffset.x < 0) {
            [self.contentScrollView setContentOffset:CGPointZero animated:YES];
        }else {
            [self.contentScrollView setContentOffset:CGPointMake(-200, 0) animated:YES];
        }
        
    }else {
        
        [self setupLeftDockMenu];
        [self.contentScrollView setContentOffset:CGPointMake(-200, 0) animated:YES];
        
    }
    
}

-(void)setupLeftDockMenu {
    
    WHYLeftDockMenu *leftDockMenu = [[WHYLeftDockMenu alloc] init];
    
    leftDockMenu.width = 150;
    leftDockMenu.height = 300;
    leftDockMenu.x = -200;
    leftDockMenu.y = (self.contentScrollView.height - leftDockMenu.height) * 0.5;
    
    [self.contentScrollView addSubview:leftDockMenu];
    
    self.leftDockMenu = leftDockMenu;
    
}

#pragma mark - 添加默认子控制器
-(void)setupDefaultViewController {
    
    WHYHeadLineViewController *headLineVC = (WHYHeadLineViewController *)self.childViewControllers.firstObject;
    headLineVC.view.frame = self.contentScrollView.frame;
    [self.contentScrollView addSubview:headLineVC.view];
    
}

#pragma mark - 设置scrollView的属性
-(void)setupScrolViewProperties {
    
    self.listScrollView.showsHorizontalScrollIndicator = NO;
    self.listScrollView.showsVerticalScrollIndicator = NO;
    self.contentScrollView.showsVerticalScrollIndicator = NO;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    
    self.contentScrollView.delegate = self;
    
    self.contentScrollView.pagingEnabled = YES;
    
    self.contentScrollView.backgroundColor = [UIColor lightGrayColor];
    
    CGFloat contentW = self.childViewControllers.count * [UIScreen mainScreen].bounds.size.width;
    self.contentScrollView.contentSize = CGSizeMake(contentW, 0);
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

#pragma mark - 加载子控制器
-(void)setupChildViewControllers {
    
    WHYHeadLineViewController *headLineVC = [WHYHeadLineViewController viewControllerWithStoryboardName:@"HeadLine" bundle:nil identifier:@"HeadLine"];
    headLineVC.newsId = self.channelList.firstObject.tid;
    headLineVC.title = self.channelList.firstObject.tname;
    [self addChildViewController:headLineVC];
    
    WHYSocietyViewController *societyVC = [WHYSocietyViewController viewControllerWithStoryboardName:@"Society" bundle:nil identifier:@"Society"];
    societyVC.newsId = self.channelList[1].tid;
    societyVC.title = self.channelList[1].tname;
    [self addChildViewController:societyVC];
    
    NSUInteger count = self.channelList.count;
    for (int i = 2; i < count; i++) {
        UIViewController *vc = [[UIViewController alloc] init];
        vc.title = self.channelList[i].tname;
        [self addChildViewController:vc];
    }
    
}

#pragma mark - 加载标题
-(void)setupTitles {
    
    NSUInteger count = self.channelList.count;
    
    CGFloat labelW = 80;
    CGFloat labelH = 30;
    CGFloat labelY = 0;
    
    for (int i = 0; i < count; i++) {
        
        WHYHomeLabel *label = [[WHYHomeLabel alloc] init];
        label.tag = i;
        [self.listScrollView addSubview:label];
        
        CGFloat labelX  = labelW * i;
        label.frame = CGRectMake(labelX, labelY, labelW, labelH);
        
        UIViewController *vc = self.childViewControllers[i];
        label.text = vc.title;
        
        [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickLabel:) ]];
        
    }
    
    self.listScrollView.contentSize = CGSizeMake(count * labelW, 0);
    
}

#pragma mark - clickLabel
-(void)clickLabel:(UITapGestureRecognizer *)recognizer {
    
    WHYHomeLabel *label = (WHYHomeLabel *)recognizer.view;
    
//    计算x方向上的偏移量
    CGFloat offsetX = label.tag * self.contentScrollView.frame.size.width;
    
//    设置偏移量
    [self.contentScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

#pragma mark - scrollView代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat value = self.contentScrollView.contentOffset.x / self.contentScrollView.width;
    
    if (value < 0) {
        return;
    }
    
    NSUInteger leftIndex = (NSUInteger)value;
    NSUInteger rightIndex = leftIndex + 1;
    
    if (rightIndex > scrollView.subviews.count) {
        return;
    }
    
    CGFloat rightScale = value - leftIndex;
    CGFloat leftSacle = 1 - rightScale;
    
    WHYHomeLabel *leftLabel = self.listScrollView.subviews[leftIndex];
    WHYHomeLabel * rightLabel = self.listScrollView.subviews[rightIndex];
    
    leftLabel.scale = leftSacle;
    rightLabel.scale = rightScale;
    
}

//使用代码设置scrollView.contentOffset的时候会调用此方法,手在屏幕上拖动scrollView不会调用
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    
    NSUInteger index = scrollView.contentOffset.x / scrollView.width;
    UIViewController *vc = self.childViewControllers[index];
    
    WHYHomeLabel *label = self.listScrollView.subviews[index];
    CGFloat offsetX = label.center.x - self.listScrollView.width * 0.5;
    
    CGFloat maxOffsetX = self.listScrollView.contentSize.width - self.listScrollView.width;
    if (offsetX < 0) {
        offsetX = 0;
    }else if (offsetX > maxOffsetX) {
        offsetX = maxOffsetX;
    }
    
    [self.listScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    
    [self.listScrollView.subviews enumerateObjectsUsingBlock:^(WHYHomeLabel *  label, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx != index) {
            label.scale = 0.0;
        }
    }];
    
    if (vc.view.superview) {
        return;
    }
    
    vc.view.width = scrollView.width;
    vc.view.height = scrollView.height;
    vc.view.x = index * vc.view.width;
    vc.view.y = 0;
    
    [self.contentScrollView addSubview:vc.view];
}

//当手在屏幕上拖动scrollView会调用此方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

#pragma mark - 设置UI
-(void)setupUI {
    
    [self.view addSubview:self.listScrollView];
    [self.view addSubview:self.contentScrollView];
    
    [self.listScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(64);
        make.height.mas_equalTo(30);
    }];
    
    [self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.listScrollView.mas_bottom);
        make.bottom.mas_equalTo(self.view).offset(-49);
    }];
    
}

#pragma mark - 懒加载
-(UIScrollView *)listScrollView {
    if (_listScrollView == nil) {
        _listScrollView = [[UIScrollView alloc] init];
    }
    return _listScrollView;
}

-(UIScrollView *)contentScrollView {
    if (_contentScrollView == nil) {
        _contentScrollView = [[UIScrollView alloc] init];
    }
    return _contentScrollView;
}

-(NSArray<WHYChannel *> *)channelList {
    if (_channelList == nil) {
        _channelList = [WHYChannel channelList];
    }
    return _channelList;
}


@end








